package com.tjsinfo.tjsoa.system.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.bo.ISystemUserDepartmentBo;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDepartment;


public class UserAllDepartmentByUseridAction extends Action{
	private ISystemUserDepartmentBo systemUserDepartmentBo;
	private ISystemUserBo systemUserBo;
	
	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	public ISystemUserDepartmentBo getSystemUserDepartmentBo() {
		return systemUserDepartmentBo;
	}

	public void setSystemUserDepartmentBo(
			ISystemUserDepartmentBo systemUserDepartmentBo) {
		this.systemUserDepartmentBo = systemUserDepartmentBo;
	}
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {
		Integer userid = SessionUser.getUserId(request);
		List<TSystemUser> systemuserlist = new ArrayList<TSystemUser>();
		List<TSystemUserDepartment> systemUserDepartmentList =  systemUserDepartmentBo.selectByUserId(userid);
		for (TSystemUserDepartment systemUserDepartment : systemUserDepartmentList) {
			
			systemuserlist.addAll(systemUserBo.selectByDepartmentNotDel(systemUserDepartment.getDepartmentId())) ;
		}
		JSONArray array = new JSONArray();
		for (TSystemUser systemUser : systemuserlist) {
			if(!Byte.valueOf("1").equals(systemUser.getState() ) ){
				JSONObject object = new JSONObject();
				object.put("userId", systemUser.getUserId());
				object.put("userName", systemUser.getUserName());
				object.put("uuid", systemUser.getUuid());
				array.put(object);
			}
			
		}
		response.getWriter().print(array);
		return null;
	}
}
